//
//  STStatusRetweetView.swift
//  STVVeibo
//
//  Created by leergou on 16/7/24.
//  Copyright © 2016年 WhiteHouse. All rights reserved.
//

import UIKit
import SnapKit

class STStatusRetweetView: UIView {
    // 记录转发微博底部 约束
    var retweetViewBottomConstranit: Constraint?
    
    //定义属性 供外界赋值
    var statusViewModel: STStatusViewModel?{
        
        didSet{
            
//            contentLabel.text = statusViewModel?.status?.retweeted_status?.text
            contentLabel.attributedText = statusViewModel?.retweetContentAttr
            
            /*
            卸载转发微博底部约束
            - 如果配图 count > 0 
            -01 给配图赋值
            -02 更改约束
            -03 显示配图
            - count = 0
            -01 更改约束
            -02 隐藏配图

            */
            // 卸载转发微博底部视图的约束
            self.retweetViewBottomConstranit?.uninstall()
            if statusViewModel?.status?.retweeted_status?.pic_urls?.count > 0 {
                // 赋值
                pictureView.pictureInfo = statusViewModel?.status?.retweeted_status?.pic_urls
                // 更改约束
                self.snp_makeConstraints(closure: { (make) -> Void in
                    self.retweetViewBottomConstranit = make.bottom.equalTo(pictureView).offset(pictureViewCellMargin).constraint
                })
                // 显示配图
                pictureView.hidden = false
            } else {
                // 没有配图
                // 更改约束
                self.snp_makeConstraints(closure: { (make) -> Void in
                    self.retweetViewBottomConstranit = make.bottom.equalTo(contentLabel.snp_bottom).offset(pictureViewCellMargin).constraint
                })
                // 隐藏配图
                pictureView.hidden = true
            }
            
        }
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MAKR: setupUI
    func setupUI(){
        backgroundColor = RGB(237, green: 237, blue: 237)
        
        // 添加子控件
        addSubview(contentLabel)
        addSubview(pictureView)
        
        // 添加约束
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.top.leading.equalTo(statusCellMargin)
        }
        pictureView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentLabel.snp_bottom).offset(pictureViewCellMargin)
            make.leading.equalTo((SCREENW - 250) * 0.5)
        }
        
        // 约束当前view
        self.snp_makeConstraints { (make) -> Void in
            self.retweetViewBottomConstranit = make.bottom.equalTo(pictureView.snp_bottom).offset(statusCellMargin).constraint
        }
    }
    
    //MAKR: lazy
    // 微博内容
    private lazy var contentLabel: UILabel = UILabel(font: 15, textColor: UIColor.darkGrayColor(), maxWidth: SCREENW - 20)
    // 配图
    private lazy var pictureView: STStatusPictureView = STStatusPictureView()
    
}

















